Git SSH

Git SSH

Really useful if you're using two or more [[Git]] accounts on a single computer. On windows I initially used the GitHub CLI tool, which allowed the use of gh auth login but this got annoying due to 2FA.

Switching to SSH made this process much easier, and it's relatively simple to get it set up.

Note

This will give you the correct permissions when pushing / pulling from a repo, but it will still use your global git config. Make sure to set git config --local user.name and git config --local user.email for each repository.

Alternatively, see [[Git Account Switch]].

Setup

First you'll need to add some files in ~/.ssh if they don't exist already:

SSH Keys

Follow the steps in the section named "Generating a new SSH Key" found in the following documentation from GitHub: Generating a new SSH key and adding it to the ssh-agent

Configure SSH

Add your accounts using a unique hostname. Set IdentityFile to the name of the key you created for each account in the previous step.

Usage

When cloning a repository, you can use each hostname depending on which account you'd like to use. For example your personal account:

And for your work account:

If you have an existing repository and want to update it to use SSH to avoid permission issues (assuming remote is origin):

Replacing @github.com with whatever hostname you'd like to use.

Auto Startup

This works great, but if you're running git commands from the command line you'll end up typing in your password a lot.

Bash

First, ensure that following lines are added to .bash_profile:

Now, add the following text to .bashrc, which should be found in your root user home folder:

Windows

By default the sshd service is set to start manually. To start it each time the server is rebooted, run the following commands from an elevated PowerShell prompt:

If you're having problems getting the service running:

Git SSH